package org.jeecg.modules.oms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.oms.entity.CfProfileWorkDetail;
import org.jeecg.modules.oms.service.ICfProfileWorkDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 长风型材加工详情控制器
 */
@Slf4j
@Api(tags = "长风型材加工详情")
@RestController
@RequestMapping("/oms/cfProfileWorkDetail")
public class CfProfileWorkDetailController extends JeecgController<CfProfileWorkDetail, ICfProfileWorkDetailService> {

    @Autowired
    private ICfProfileWorkDetailService cfProfileWorkDetailService;

    /**
     * 根据型材ID获取加工详情
     */
    @AutoLog(value = "长风型材加工详情-根据型材ID获取")
    @ApiOperation(value = "长风型材加工详情-根据型材ID获取", notes = "根据型材ID获取加工详情")
    @GetMapping(value = "/getByProfileId")
    public Result<List<CfProfileWorkDetail>> getByProfileId(@RequestParam(name = "profileId") String profileId) {
        try {
            log.info("获取型材加工详情, profileId={}", profileId);
            List<CfProfileWorkDetail> workDetails = cfProfileWorkDetailService.getByProfileId(profileId);
            return Result.OK(workDetails);
        } catch (Exception e) {
            log.error("获取型材加工详情失败", e);
            return Result.error("获取型材加工详情失败: " + e.getMessage());
        }
    }

    /**
     * 根据切割ID获取加工详情
     */
    @AutoLog(value = "长风型材加工详情-根据切割ID获取")
    @ApiOperation(value = "长风型材加工详情-根据切割ID获取", notes = "根据切割ID获取加工详情")
    @GetMapping(value = "/getByCutId")
    public Result<List<CfProfileWorkDetail>> getByCutId(@RequestParam(name = "cutId") String cutId) {
        try {
            log.info("获取切割加工详情, cutId={}", cutId);
            List<CfProfileWorkDetail> workDetails = cfProfileWorkDetailService.getByCutId(cutId);
            return Result.OK(workDetails);
        } catch (Exception e) {
            log.error("获取切割加工详情失败", e);
            return Result.error("获取切割加工详情失败: " + e.getMessage());
        }
    }

    /**
     * 根据订单ID获取加工详情
     */
    @AutoLog(value = "长风型材加工详情-根据订单ID获取")
    @ApiOperation(value = "长风型材加工详情-根据订单ID获取", notes = "根据订单ID获取加工详情")
    @GetMapping(value = "/getByOrderId")
    public Result<List<CfProfileWorkDetail>> getByOrderId(@RequestParam(name = "orderId") String orderId) {
        try {
            log.info("获取订单加工详情, orderId={}", orderId);
            List<CfProfileWorkDetail> workDetails = cfProfileWorkDetailService.getByOrderId(orderId);
            return Result.OK(workDetails);
        } catch (Exception e) {
            log.error("获取订单加工详情失败", e);
            return Result.error("获取订单加工详情失败: " + e.getMessage());
        }
    }

    /**
     * 分页查询加工详情
     */
    @AutoLog(value = "长风型材加工详情-分页查询")
    @ApiOperation(value = "长风型材加工详情-分页查询", notes = "长风型材加工详情-分页查询")
    @GetMapping(value = "/page")
    public Result<IPage<CfProfileWorkDetail>> page(
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "orderId", required = false) String orderId,
            @RequestParam(name = "profileId", required = false) String profileId,
            @RequestParam(name = "cutId", required = false) String cutId,
            @RequestParam(name = "windowNo", required = false) String windowNo,
            @RequestParam(name = "barCode", required = false) String barCode) {
        
        Page<CfProfileWorkDetail> page = new Page<>(pageNo, pageSize);
        LambdaQueryWrapper<CfProfileWorkDetail> queryWrapper = new LambdaQueryWrapper<>();
        
        if (orderId != null && !orderId.isEmpty()) {
            queryWrapper.eq(CfProfileWorkDetail::getOrderId, orderId);
        }
        
        if (profileId != null && !profileId.isEmpty()) {
            queryWrapper.eq(CfProfileWorkDetail::getProfileId, profileId);
        }
        
        if (cutId != null && !cutId.isEmpty()) {
            queryWrapper.eq(CfProfileWorkDetail::getCutId, cutId);
        }
        
        if (windowNo != null && !windowNo.isEmpty()) {
            queryWrapper.eq(CfProfileWorkDetail::getWindowNo, windowNo);
        }
        
        if (barCode != null && !barCode.isEmpty()) {
            queryWrapper.eq(CfProfileWorkDetail::getBarCode, barCode);
        }
        
        IPage<CfProfileWorkDetail> pageList = cfProfileWorkDetailService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
} 